#include "tthreadlist.h"
#include "tthreadlistitem.h"
#include "iostream"
#include "tkeypresseater.h"

TThreadList::TThreadList(QTreeWidget *parent) :
    QTreeWidget(parent)
{
    this->setSelectionMode(QAbstractItemView::ExtendedSelection);
    this->setColumnCount(3);
    QStringList head;
    head << "Name" << "Status" <<"URL";
    this->setColumnHidden(2, true);
    this->setColumnWidth(0,300);
    this->setHeaderLabels(head);
    //addTopLevelItem(new TThreadListItem());

    this->setContextMenuPolicy(Qt::CustomContextMenu);
    QObject::connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
    //TKeyPressEater *keyPressEater = new TKeyPressEater(this);
    //this->installEventFilter(keyPressEater);
}

void TThreadList::addThread(QUrl threadUrl, QString threadName){
    std::cout << "Echo 3" << std::endl;
    if (threadName.isNull()){
        threadList.append(new TThreadListItem(threadUrl, NULL));
    }else{
        threadList.append(new TThreadListItem(threadUrl, threadName));
    }
    QObject::connect(threadList.last(), SIGNAL(isChanged()), this, SLOT(save()));
    this->addTopLevelItem(threadList.last());
    threadList.last()->activate();
    std::cout << "Echo 8" << std::endl;
}

void TThreadList::deleteThread(int threadIndex){
    delete threadList.at(threadIndex);
    threadList.removeAt(threadIndex);
}

void TThreadList::startWatch(int threadIndex){
    threadList.at(threadIndex)->startWatch();
}

void TThreadList::stopWatch(int threadIndex){
    threadList.at(threadIndex)->stoptWatch();
}

void TThreadList::showMenu(QPoint point){
    std::cout << "Context Menu" << std::endl;
    for (int i=0; i < this->topLevelItemCount(); i++){
        if (this->topLevelItem(i)->isSelected()){
            std::cout << "Item at \"" << i << "\" is selected" <<std::endl;
	    QMenu * menu = new QMenu(this);
	    menu->move(QCursor::pos());
            menu->addAction("Rename");
            QObject::connect(menu->actions().at(0), SIGNAL(triggered()), this, SLOT(renameThread()));
            menu->show();
        }
    }
}


void TThreadList::renameThread(void){
    std::cout << "Rename" << std::endl;
    QDialog* renameWidget = new QDialog();
    QLineEdit* lineEdit = new QLineEdit(renameWidget);
    QPushButton* okayButton = new QPushButton("Okay");
    QPushButton* cancelButton = new QPushButton("Canсel");
    QGridLayout* grid = new QGridLayout();
    renameWidget->setLayout(grid);
    grid->addWidget(lineEdit, 0,0, 1,4);
    lineEdit->setText(this->currentItem()->text(0));
    grid->addWidget(okayButton, 1,1);
    QObject::connect(okayButton, SIGNAL(clicked()), renameWidget, SLOT(accept()));
    grid->addWidget(cancelButton, 1,2);
    QObject::connect(cancelButton, SIGNAL(clicked()), renameWidget, SLOT(reject()));

    renameWidget->setWindowTitle("Thread rename");
    renameWidget->setMinimumWidth(300);
    renameWidget->setModal(true);
    if (renameWidget->exec()){
        std::cout << "Renamed to \"" << lineEdit->text().toStdString() <<"\"" << std::endl;
        this->threadList.at(this->currentIndex().row())->rename(lineEdit->text());
    }else{
        std::cout << "Rename canceled" << std::endl;
    }
}


void TThreadList::save(void){
    QStringList urlist;
    QUrl currentUrl;
    QStringList urlnames;
    for (int i=0; i < this->threadList.count(); i++){
        currentUrl = this->threadList.at(i)->getURL();
        urlist.append(currentUrl.toString());
	urlnames.append(this->threadList.at(i)->getName());
    }
    QSettings settings;
    settings.beginGroup("Urls");
    settings.setValue("UrlsList", urlist);
    settings.setValue("UrlsNamesList", urlnames);
    settings.endGroup();
}
